package com.xywh.micro_warehousing.mvp.ui.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.utils.LogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.xywh.micro_warehousing.R;
import com.xywh.micro_warehousing.mvp.ui.entity.Tab1;
import com.xywh.micro_warehousing.mvp.ui.util.MapUtil;

import java.util.List;

import me.jessyan.armscomponent.commonres.view.RecycleViewDivider;

/**
 * @ProjectName:
 * @ackage: com.xywh.micro_warehousing.mvp.ui.adapter
 * @ClassName: CountersAdapter
 * @Description: 二级：柜子名称和距离
 * @Author: 小星
 * @CreateDate: 2020/6/22 0022 14:08
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/6/22 0022 14:08
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class CountersAdapter extends BaseQuickAdapter<Tab1.DataBean.CountersBean, BaseViewHolder> {

    private Context mContext;
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    public CountersAdapter(Context context, @Nullable List<Tab1.DataBean.CountersBean> data) {
        super(R.layout.item_counters, data);
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, Tab1.DataBean.CountersBean item) {
        //GlideUtil.loadImage(mContext,item.getImg(),helper.getView(R.id.img_head));
        helper.setText(R.id.tv_ming_cheng, "柜子名称：" + item.getCounterName());
        helper.setText(R.id.tv_ju_li, "距离：" + item.getLineDistance());
        helper.setOnClickListener(R.id.tv_ju_li, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.debugEInfo("item.getLatitude()==" + item.getLatitude());
                LogUtils.debugEInfo("item.getLongitude()==" + item.getLongitude());
                if (!TextUtils.isEmpty(item.getLatitude()) && !TextUtils.isEmpty(item.getLongitude()))
                    showSimpleBottomSheetList(Double.parseDouble(item.getLatitude())
                            , Double.parseDouble(item.getLongitude()),
                            item.getCounterAdd());
            }
        });
        if (item.getMaterials() != null && item.getMaterials().size() > 0) {

            RecyclerView recyclerView = helper.getView(R.id.recyclerView);
            recyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, 0, mContext.getResources().getColor(R.color.public_client_line_color)));
            MaterialsAdapter mAdapter = new MaterialsAdapter(mContext, item.getMaterials(), item.getCounterId());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(mAdapter);
        }

    }

    private void showSimpleBottomSheetList(double latx, double laty, String mAddress) {

        LogUtils.debugEInfo("latx=" + latx + "  laty=" + laty);
        final String[] items = new String[]{"腾讯地图", "百度地图", "高德地图"};
        new QMUIDialog.MenuDialogBuilder(mContext)
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        switch (which) {
                            case 2:
                                if (MapUtil.isGdMapInstalled()) {
                                    MapUtil.openGaoDeNavi(mContext, 0, 0, null, latx, laty, mAddress);
                                } else {
                                    //这里必须要写逻辑，不然如果手机没安装该应用，程序会闪退，这里可以实现下载安装该地图应用
                                    Toast.makeText(mContext, "尚未安装高德地图", Toast.LENGTH_SHORT).show();
                                }
                                break;
                            case 1:
                                if (MapUtil.isBaiduMapInstalled()) {
                                    MapUtil.openBaiDuNavi(mContext, 0, 0, null, latx, laty, mAddress);
                                } else {
                                    Toast.makeText(mContext, "尚未安装百度地图", Toast.LENGTH_SHORT).show();
                                }
                                break;
                            case 0:
                                if (MapUtil.isTencentMapInstalled()) {
                                    MapUtil.openTencentMap(mContext, 0, 0, null, latx, laty, mAddress);
                                } else {
                                    Toast.makeText(mContext, "尚未安装腾讯地图", Toast.LENGTH_SHORT).show();
                                }
                                break;
                        }

                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

}
